// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
let db = uniCloud.database();
module.exports = {
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	async wx_login(code) {
		// 参数校验，如无参数则不需要
		if (!code) {
			return {
				errCode: 4000,
				errMsg: '网络故障，请重新操作！'
			}
		}
		const login_res = await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/jscode2session", {
			method: 'POST',
			data: {
				appid: 'wx23a3c312990236c8',
				secret: '3db3d7b88f7ba81bf223f2a8ccce2dd7',
				js_code: code,
				grant_type: 'authorization_code'
			},
			dataType: 'json',
			header: {
				'content-type': 'application/json'
			}
		})
		if (login_res.data.errcode) {
			return {
				errCode: 4002,
				errMsg: '登录失败，请重新登录！'
			}
		}
		return login_res;
	},
	async baseLogin(parms) {
		let res = await db.collection("roomcenteruser").where({
			name: parms.name,
			pwd: parms.pwd
		}).get();
		return res;
	},
	async getWxUser(openid) {
		let res = await db.collection("roomcenteruser").where({
			openid: openid
		}).get();
		return res;
	}
}